package com.woniuxy.microcontentserver.messagemsg.mapper;

import com.woniuxy.microcontentserver.bean.MessageBean;
import com.woniuxy.microcontentserver.bean.SectionBean;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.Date;
import java.util.List;

@Mapper
public interface MessageMapper {
   /* *//**
     * 查找所有管理员
     * @return
     *//*
    @Results({
            @Result(property = "departmentInfo",column = "fk_department",javaType = DepartmentInfo.class,one =
            @One(select = "com.woniuxy.microsysserver.mapper.DepartmentMapper.findById",fetchType = FetchType.LAZY)),
            @Result(property = "roleInfo",column = "fk_role",javaType = RoleInfo.class,one =
            @One(select = "com.woniuxy.microsysserver.mapper.RoleMapper.findById",fetchType = FetchType.LAZY))
    })
    @Select("select id,login_name as loginName,admin_name as adminName,fk_department,fk_role," +
            "create_time as createTime,phone from admin_info where state = 1")
    List<AdminInfo> findAll();
    */
    /**
     * 查询所有资讯
     * @param
     * @return 将前端传来的参数封装进messsageBean对象中
     */
    @Results({
            @Result(property = "sectionBean",column = "fk_section_id",javaType = SectionBean.class,one =
            @One(select = "com.woniuxy.microcontentserver.sectionmsg.mapper.SectionMapper.getOneById",fetchType = FetchType.LAZY))
    })
    /*@Select("select id,name,author,status,image,source,describe,text,issue_time as issueTime," +
            "is_top as isTop,is_pass as isPass,fk_section_id from message_info")*/
    @Select("select * from message_info")
    List<MessageBean> findAll();

    /**
     * 根据id查询资讯
     * @param id
     * @return
     */
    @Results({
            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property = "name", column = "name", javaType = String.class),
            @Result(property = "author", column = "author", javaType = String.class),
            @Result(property = "status", column = "status", javaType = Integer.class),
            @Result(property = "image", column = "image", javaType = String.class),
            @Result(property = "source", column = "source", javaType = String.class),
            @Result(property = "describe", column = "describe", javaType = String.class),
            @Result(property = "text", column = "text", javaType = String.class),
            @Result(property = "issueTime", column = "issue_time", javaType = Date.class),
            @Result(property = "isTop", column = "is_top", javaType = Integer.class),
            @Result(property = "isPass", column = "is_pass", javaType = Integer.class),

            @Result(property = "sectionBean", column = "fk_section_id", javaType = SectionBean.class, one =
            @One(select = "com.woniuxy.microcontentserver.sectionmsg.mapper.SectionMapper.getOneById", fetchType = FetchType.LAZY))
    })
    @Select("select * from message_info where id=#{id}")
    MessageBean getOneById(Long id);

   /* *//**
     * 新增管理员
     * @param adminInfo
     *//*
    @Insert("insert into admin_info (login_name,admin_name,fk_department,fk_role,create_time," +
            "state,phone,password) values (#{a.loginName},#{a.adminName},#{a.departmentInfo.id},#{a.roleInfo.id}," +
            "now(),1,#{a.phone},#{a.password})")
    void saveAdminInfo(@Param("a") AdminInfo adminInfo);*/

    /**
     * 新增资讯
     * @param messageBean
     * @return
     */

    /*@Options(
            useGeneratedKeys = true,
            keyProperty = "a.id"
    )
    @Insert("insert into advertise_info"+
            "(name,image,jump_address,position,status,advertise_time)"+
            "values(#{a.name},#{a.image},#{a.jumpAddress},#{a.position},#{a.status},#{a.advertiseTime})"
    )
    int addAdvertiseBean(@Param("a") AdvertiseBean advertiseBean);*/

    @Options(
            useGeneratedKeys = true,
            keyProperty = "m.id"
    )
    @Insert("insert into message_info" +
            " (name,author,status,image,source,describe" +
            "text,issue_time,is_top,is_pass,fk_section_id) " +
            "values (null,#{m.name},#{m.author},#{m.status},#{m.image},#{m.source},#{m.describe}," +
            "#{m.text},now(),#{m.isTop},#{m.isPass},#{m.sectionBean.id})")
    int addMessageBean(@Param("m") MessageBean messageBean);

    /**
     * 修改资讯
     * @param messageBean
     * @return
     */
    @Update("<script>"+
            "update message_info"+
            "<set>"+

            "<if test='m.name != null'>" +
            " name = #{m.name}," +
            "</if>" +

            "<if test='m.author != null'>" +
            " author = #{m.author}," +
            "</if>" +

            "<if test='m.status != null'>" +
            " status = #{m.status}," +
            "</if>" +

            "<if test='m.image != null'>" +
            " image = #{m.image}," +
            "</if>" +

            "<if test='m.source != null'>" +
            " source = #{m.source}," +
            "</if>" +

            "<if test='m.describe != null'>" +
            " describe = #{m.describe}," +
            "</if>" +

            "<if test='m.text != null'>" +
            " text = #{m.text}," +
            "</if>" +

            "<if test='m.issueTime != null'>" +
            " issue_time = #{m.issueTime}," +
            "</if>" +

            "<if test='m.isTop != null'>" +
            " is_top = #{m.isTop}," +
            "</if>" +

            "<if test='m.isPass != null'>" +
            " is_pass = #{m.isPass}," +
            "</if>" +

            "</set>"+
            "where id=#{m.id}"+
            "</script>"
    )
    int updateMessageBean(@Param("m") MessageBean messageBean);

    /**
     * 删除资讯
     * @param id
     * @return
     */
    @Delete("delete from message_info where id=#{id}")
    int deleteMessageBean(@Param("id") Long id);


}
